#include <iostream>


class CyclicQueue {
private:
	int* arr;
	int size;
	int front;
	int rear;

public:
	CyclicQueue(int capacity) {
		size = capacity + 1;
		arr = new int[size];

		front = 0;
		rear = 0;
	}

public:
	~CyclicQueue() {
		delete[] arr;
		arr = nullptr;
	}


	bool Enqueue(int val) {
		if (IsFull()) {
			return false;
		}


		//rear会越来越大，arr[rear]就会越界，因为数组的大小是提前制定的
		//arr[rear] = val;
		arr[rear] = val;
		rear = (rear + 1) % size;

		return true;
	}


	int Dequeue() {
		if (IsEmpty()) {
			throw - 1;
		}

		int val = arr[front];

		front = (front + 1) % size;

		return val;
	}

	bool IsFull() const {
		return (rear + 1) % size == front;
	}

	bool IsEmpty() const {
		return front == rear;
	}

	void Print() const{
		int i = front;
		while (i != rear) {
			std::cout << arr[i] << " ";
			i = (i + 1) % size;
		}
		std::cout << "\n";
	}

};

int main() {

	CyclicQueue q(5);


	q.Enqueue(1);
	q.Enqueue(2);
	q.Enqueue(3);
	q.Enqueue(4);
	q.Enqueue(5);
	q.Enqueue(6);

	q.Print();

	std::cout << q.Dequeue() << std::endl;
	std::cout << q.Dequeue() << std::endl;
	//std::cout << q.Dequeue() << std::endl;
	//std::cout << q.Dequeue() << std::endl;
	//std::cout << q.Dequeue() << std::endl;

	q.Enqueue(77);
	q.Enqueue(88);
	q.Enqueue(99);

	while (!q.IsEmpty()) {
		std::cout << q.Dequeue() << std::endl;
	}


	return 0;
}